package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.model.*;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.SpecService;
import com.dongdongshop.vo.SpecVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {
    @Resource
    private TbSpecificationMapper sm;
    @Resource
    private TbSpecificationOptionMapper om;
    //查询
    @Override
    public List<SpecVo> getList() {
        List<TbSpecification> tbspec = sm.selectByExample(null);
        List<SpecVo> vo = JSONObject.parseArray(JSONObject.toJSONString(tbspec), SpecVo.class);
        return vo;
    }

    //增加

    @Override
    public void addSpecAndOption(SpecVo vo, List<TbSpecificationOption> optionList) {
        TbSpecification spec = new TbSpecification();
        BeanUtils.copyProperties(vo,spec);
        //先保存规格表,并且返回主键
        sm.insertSelective(spec);
        long id = spec.getId();
        //批量增加
        om.insertFor(optionList, id);
    }

    //回显

    @Override
    public SpecVo toupdate(Long id) {
        //根据Id查出sm中的数据
        TbSpecification spec = sm.selectByPrimaryKey(id);
        //查另一张表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> o = om.selectByExample(example);
        //放入到Vo类中
        SpecVo vo = new SpecVo();
        if (spec != null ){
            BeanUtils.copyProperties(spec,vo);
            vo.setSpecOptionsList(o);
        }
        return vo;
    }

    //修改

    @Override
    public void updateSpec(SpecVo vo) {
        TbSpecification on= new TbSpecification();
        BeanUtils.copyProperties(vo,on);
        sm.updateByPrimaryKeySelective(on);
        List<TbSpecificationOption> ops = JSONObject.parseArray(vo.getSpecOptions(), TbSpecificationOption.class);
        //先删除 后批量增加
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(vo.getId());
        om.deleteByExample(example);
        //批量增加
        om.insertFor(ops, vo.getId());
    }

    @Override
    public PageResult specPage(PageParms<SpecVo> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<TbSpecification> tbSpec = sm.selectByExample(null);
        PageInfo pageInfo = new PageInfo(tbSpec);
        List<SpecVo> specVoList = tbSpec.stream().map(spec -> {
            SpecVo vo = new SpecVo();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult(page.getPageNum(),page.getPageSize(),specVoList,pageInfo.getTotal());
    }

    @Override
    public void deleteSpec(Long[] ids) {
        for (Long id : ids) {
            sm.deleteByPrimaryKey(id);
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            om.deleteByExample(example);
        }
    }
}
